package com.thinkgem.jeesite.kpoint.core.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.thinkgem.jeesite.modules.sys.entity.SysUser;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * @Author:Kongxiangsheng
 * @DesCription:
 * @Date:Created in 19:00 2018/8/8
 * @Modified By:
 */
public class KpointMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取用户
        String userId = "anonymous";
        SysUser user = UserUtils.getUser();
        if (StringUtils.isNotBlank(user.getId())) {
            userId = user.getId();
        }
        // 获取时间
        Date date = new Date();

        Object createBy = getFieldValByName("createBy", metaObject);
        if (null == createBy) {
            setFieldValByName("createBy", userId, metaObject);
        }
        Object createDate = getFieldValByName("createDate", metaObject);
        if (null == createDate) {
            setFieldValByName("createDate", date, metaObject);
        }
        Object updateBy = getFieldValByName("updateBy", metaObject);
        if (null == updateBy) {
            setFieldValByName("updateBy", userId, metaObject);
        }
        Object updateDate = getFieldValByName("updateDate", metaObject);
        if (null == updateDate) {
            setFieldValByName("updateDate", date, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取用户
        String userId = "anonymous";
        SysUser user = UserUtils.getUser();
        if (StringUtils.isNotBlank(user.getId())) {
            userId = user.getId();
        }
        // 获取时间
        Date date = new Date();

        setFieldValByName("updateBy", userId, metaObject);
        setFieldValByName("updateDate", date, metaObject);

    }
}
